require "Common/define"
require "Logic/Config"
require "Common/functions"
---@class PasteFaceNoticeManager
PasteFaceNoticeManager = PasteFaceNoticeManager or {};
local this = PasteFaceNoticeManager;

local waitPopTips;
local popTipsIndex = 0;

function PasteFaceNoticeManager.init()

end

function PasteFaceNoticeManager.OnDestroy()

end

function PasteFaceNoticeManager.resetPasteFaceNotice()
    this.waitPopTips = {};
end

function PasteFaceNoticeManager.startPasteFaceNotice()
    if logicMgr.GuideMgr.IsGuiding() then
        waitPopTips = {};
        logicMgr.ActiveManager.startActivePopUpTips();
        return;
    end
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Cost) then
        waitPopTips = {{}, {}, {}};
        local loginNotice = G.dataTable["t_loginnotice"];
        for k, v in pairs(loginNotice) do
            if tonumber(v.f_NoticeShow) == 1 then
                if v.f_FunctionSwitchID == nil or v.f_FunctionSwitchID == "" or tonumber(v.f_FunctionSwitchID) == 0 or logicMgr.FuncOpenManager.isOpen(v.f_FunctionSwitchID) then
                    local noticeTyp = tonumber(v.f_NoticeType);
                    local activityTime = 0;
                    local activityEndTime = 0;
                    local startTime = 0;
                    local endTime = 0;
                    if noticeTyp == 1 then
                        --集市
                        local costData = logicMgr.CostManager.getActiveServerDataList(v.f_NoticeID);
                        if costData ~= nil and costData.startTime > 0 then
                            activityTime = costData.startTime;
                            activityEndTime = costData.drawEndTime;
                            startTime = activityTime - (tonumber(v.f_NoticeAdvance) * 24 * 3600);
                            endTime = activityTime + (tonumber(v.f_NoticeDay) * 24 * 3600);
                        end
                    elseif noticeTyp == 2 then
                        --活动
                        if logicMgr.ActiveManager.checkActiveOpen(v.f_NoticeID) then
                            local activityData = logicMgr.ActiveManager.getActiveServerDataList(v.f_NoticeID);
                            if activityData ~= nil and activityData.startTime > 0 then
                                activityTime = activityData.startTime;
                                activityEndTime = activityData.drawEndTime;
                                startTime = activityTime - (tonumber(v.f_NoticeAdvance) * 24 * 3600);
                                endTime = activityTime + (tonumber(v.f_NoticeDay) * 24 * 3600);
                            end
                        end
                    elseif noticeTyp == 3 then
                        --先预留
                    end

                    local needShow = false;
                    local noticeShowType = tonumber(v.f_NoticeShowType);
                    if noticeShowType == 0 then
                        if PasteFaceNoticeManager.getPasteFaceNoticeStatus(v.f_ID) then
                            needShow = true;
                        end
                    elseif noticeShowType == 1 then
                        needShow = true;
                    elseif noticeShowType == 2 then
                        local hide = GetPlayerPrefs("pasteFaceNotice"..logicMgr.HeroManager.GetRoleInfo().playerId..v.f_NoticeID);
                        if hide then
                            if activityEndTime > 0 and HelperFunc.GetServerTime() > tonumber(hide) then
                                needShow = true;
                                SetPlayerPrefs("pasteFaceNotice"..logicMgr.HeroManager.GetRoleInfo().playerId..v.f_NoticeID, activityEndTime);
                            end
                        else
                            needShow = true;
                        end
                    end
                    if needShow then
                        local serverTime = HelperFunc.GetServerTime();
                        if serverTime > startTime and serverTime < endTime then
                            local data = {};
                            data.id = v.f_ID;
                            data.sort = v.f_NoticeOrder;
                            data.time = activityTime;
                            data.endTime = activityEndTime;
                            local noticeTier = tonumber(v.f_NoticeTier);
                            if waitPopTips[noticeTier] == nil then
                                waitPopTips[noticeTier] = {};
                            end
                            table.insert(waitPopTips[noticeTier], data);
                        end
                    end
                end
            end
        end
        PasteFaceNoticeManager.showPasteFaceNotice();
    else
        waitPopTips = {};
        logicMgr.ActiveManager.startActivePopUpTips();
        return;
    end
end

function PasteFaceNoticeManager.showPasteFaceNotice()
    if waitPopTips ~= nil and #waitPopTips > 0 then
        local data = waitPopTips[1];
        table.remove(waitPopTips, 1);
        popTipsIndex = popTipsIndex + 1;
        if data ~= nil and #data > 0 then
            local name;
            if popTipsIndex == 1 then
                name = "UILoginNotice";
            elseif popTipsIndex == 2 then
                name = "UILoginNoticeTwo";
            elseif popTipsIndex == 3 then
                name = "UILoginNoticeThree";
            end
            if name == nil or name == "" then
                this.showPasteFaceNotice();
            else
                showUI(name, data);
            end
        else
            this.showPasteFaceNotice();
            return;
        end
    else
        logicMgr.ActiveManager.startActivePopUpTips();
    end
end

function PasteFaceNoticeManager.setPasteFaceNoticeStatus(loginNotice, activityEndTime)
    if loginNotice == nil then
        return;
    end
    local noticeShowType = tonumber(loginNotice.f_NoticeShowType);
    if noticeShowType == 0 then
        PasteFaceNoticeManager.updatePasteFaceNoticeStatus(loginNotice.f_ID);
    elseif noticeShowType == 2 then
        SetPlayerPrefs("pasteFaceNotice" .. logicMgr.HeroManager.GetRoleInfo().playerId .. loginNotice.f_NoticeID, activityEndTime);
    end
end

function PasteFaceNoticeManager.updatePasteFaceNoticeStatus(id)
    if GetPlayerPrefsAfter5AMFlag("pasteFaceNotice"..logicMgr.HeroManager.GetRoleInfo().playerId.."pasteFaceId"..id) then
        SetPlayerPrefsAfter5AMFlag("pasteFaceNotice"..logicMgr.HeroManager.GetRoleInfo().playerId.."pasteFaceId"..id)
    end
end

function PasteFaceNoticeManager.getPasteFaceNoticeStatus(id)
    if GetPlayerPrefsAfter5AMFlag("pasteFaceNotice"..logicMgr.HeroManager.GetRoleInfo().playerId.."pasteFaceId"..id) then
        return true;
    else
        return false;
    end
end

return PasteFaceNoticeManager